<?php if (!defined('NOVA_RUN')) die('You can\'t access this file directly!');

/*
-------------------------------------------------------
*
*   NovaBoard 2
*   Copyright © 2011 NovaBoard team
*
*--------------------------------------------------------
*
*   Official site: www.novaboard.net
*   Contact e-mail: support@novaboard.net
*
*   GNU General Public License, version 2:
*   http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
---------------------------------------------------------
*/

class lang
{
	protected static $instance;

	private $lang;
	
	public static function instance()
	{
		if (!(self::$instance instanceof lang))
		{
			self::$instance = new lang();
		}
		
		return self::$instance;
	}
	
	public function __construct()
	{
		if (!defined('NOVA_ADMIN'))
		{
			$_lang = get_cookie('lang');
			if ($_lang && file_exists(ROOT . 'languages/'.$_lang.'.lang.php'))
			{
				require ROOT . 'languages/'.$_lang.'.lang.php';
				$this->lang = $$_lang;
				setcookie('lang', $_lang, time() + 3600*24*30*12, '/');
			}
			else
			{
				if (file_exists(ROOT . 'languages/english_en.lang.php'))
				{
					require ROOT . 'languages/english_en.lang.php';
					$this->lang = $english_en;
					setcookie('lang', 'english_en', time() + 3600*24*30*12, '/');
				}
				else
				{
					throw new Exception('File '.ROOT . 'languages/english_en.lang.php is corrupt!');
				}
			}
		}
		else
		{
			$_lang = get_cookie('lang');
			if ($_lang && file_exists(ROOT . 'languages/'.$_lang.'.admin.lang.php'))
			{
				require ROOT . 'languages/'.$_lang.'.admin.lang.php';
				$this->lang = $$_lang;
				setcookie('lang', $_lang, time() + 3600*24*30*12, '/');
			}
			else
			{
				if (file_exists(ROOT . 'languages/english_en.admin.lang.php'))
				{
					require ROOT . 'languages/english_en.admin.lang.php';
					$this->lang = $english_en;
					setcookie('lang', 'english_en', time() + 3600*24*30*12, '/');
				}
				else
				{
					throw new Exception('File '.ROOT . 'languages/english_en.admin.lang.php is corrupt!');
				}
			}
		}
	}

	public function get($key1, $key2)
	{
		return @$this->lang[$key1][$key2];
	}
}